package groups;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import lecture.LectureManagedBean;
import user.User;
import user.UserAdminManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */

@ManagedBean(name="assiginingBean")
@CustomScoped(value="#{window}")
public class AssigningManagedBean {
    
    public List<UserAssigningManagedBean> users;
    
    public AssigningManagedBean() {
        this.initList();
    }

    private void initList() {
        FacesContext context = FacesContext.getCurrentInstance();
        LectureManagedBean lb = context.getApplication().evaluateExpressionGet(context, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        UserAdminManagedBean ua = context.getApplication().evaluateExpressionGet(context, "#{userAdminBean}", UserAdminManagedBean.class);
        
        this.users = new ArrayList<UserAssigningManagedBean>();
        List<User> addedUser = new ArrayList<User>();
        for (ExerciseGroup eg: lecture.getInfo().getExerciseGroups()) {
            for (String s: eg.getStudents()) {
                System.out.println("Student: " + s);
                User u = ua.getUser(s);
                this.users.add(new UserAssigningManagedBean(u,eg, true));
                addedUser.add(u);
            }
        }       
        List <UserAssigningManagedBean> dontTakePartUser = new ArrayList<UserAssigningManagedBean>();
        List<User> restUser = new ArrayList<User>();
        restUser.addAll(lecture.getStudents()); 
        restUser.removeAll(addedUser);
        
        for (User u: restUser) {
            if (lecture.getWish(u.getUsername()) == null) {
                dontTakePartUser.add(new UserAssigningManagedBean(u,null,false));
            } else {
                this.users.add(new UserAssigningManagedBean(u,null,true));
            }
        }
        
        this.users.addAll(dontTakePartUser);
        this.sort();
        
    }
    
    public void sort() {
        Collections.sort(users,new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                UserAssigningManagedBean bean1 = (UserAssigningManagedBean) o1;
                UserAssigningManagedBean bean2 = (UserAssigningManagedBean) o2;
                
                if (!bean1.isTakePart()) {
                    if (!bean2.isTakePart())
                        return 0;
                    else
                        return 1;
                } else if (!bean2.isTakePart()) 
                    return -1;            
               
                if(bean1.getEg() == null && bean2.getEg() == null) {
                    return bean1.getUser().getUsername().compareToIgnoreCase(bean2.getUser().getUsername());
                } else if  (bean1.getEg() == null) {
                    return 1;
                } else if (bean2.getEg() == null) {
                    return -1;
                }
                
                int compare = (bean1.getEg().getName().compareTo(bean2.getEg().getName()));
                
                if (compare != 0 ) {
                    return compare;
                } else {
                    return bean1.getUser().getUsername().compareToIgnoreCase(bean2.getUser().getUsername());
                }
                                
            }
        });
    }

    public List<UserAssigningManagedBean> getUsers() {
        this.sort();
        return users;
    }            
    
    public List<SelectItem> getExerciseGroupsItems() {
        FacesContext context = FacesContext.getCurrentInstance();
        LectureManagedBean lb = context.getApplication().evaluateExpressionGet(context, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        
        List<SelectItem> items = new ArrayList<SelectItem>();
        items.add(new SelectItem(null,"Keine Übungsgruppe"));
        for (ExerciseGroup eg: lecture.getInfo().getExerciseGroups()) {
            items.add(new SelectItem(eg,eg.getName()));
        }
        return items;        
    }
}
